﻿Public Class ProjectForm
    ' PARA PABLO QUE LO MIRA POR TV
    Public project As New Project
    Dim stateForm As ProjectStateForm


    Public Sub New()
        InitializeComponent()
        stateForm = getProyectState()
        ' New StateProjectFormCreating(Me)
    End Sub

    Public Sub New(ByRef projectParam As Project)
        InitializeComponent()
        project = projectParam
        stateForm = getProyectState()
        ' New StateProjectFormQueried(Me)
    End Sub

    Private Sub ProjectForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtProjectName.Focus()

        'Controla rangos de fechas
        configurarRangoFechas(dtpStartDate, dtpEndDate)
        configurarRangoFechas(dtpFromDate, dtpToDate)
        configurarRangoFechas(dtpEstimatedStartDate, dtpEstimatedEndDate)

        stateForm.setForm()
        stateForm.loadData()
        stateForm.setComboAndGrids()

        'If project.stateProperty IsNot Nothing Then
        'loadFormToEdit()
        'stateForm = New StateProjectFormQueried(Me)
        'stateForm.setForm()
        'stateForm.loadData()
        'End If

        'If project IsNot Nothing Then
        ' 'loadFormToEdit()
        'stateForm = New StateProjectFormQueried(Me)
        'stateForm.setForm()
        'stateForm.loadData()
        'action = stateEnum.edit
        'End If

    End Sub

    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

        Dim resource As String = cboResources.Text
        Dim dateFrom As String = Me.dtpFromDate.Text
        Dim dateTo As String = Me.dtpToDate.Text
        Dim resourceId As Integer = Me.cboResources.SelectedValue

        For Each item As DataRowView In Me.lbAvailableRoles.SelectedItems
            Dim row As DataRow = item.Row
            Me.dgvProjectMembers.Rows.Add(resourceId, resource, CInt(row(0)), CStr(row(1)), dateFrom, dateTo)
        Next
        Me.lbAvailableRoles.ClearSelected()

    End Sub

    Private Function validateRequiredFields() As Boolean

        If txtProjectName.Text = "" Then
            MsgBox("Debe ingresar un nombre al Proyecto", MsgBoxStyle.Critical, vbYes)
            tabGeneralData.Focus()
            Return False
        End If

        If rtxtDescription.Text = "" Then
            MsgBox("Debe ingresar una breve Descripcion", MsgBoxStyle.Critical, vbYes)
            rtxtDescription.Focus()
            Return False
        End If

        If cboProjectResponsibility.SelectedIndex = -1 Then
            MsgBox("Seleccione un Recurso", MsgBoxStyle.Critical, vbYes)
            cboProjectResponsibility.Focus()
            Return False
        End If

        If cboResources.SelectedIndex = -1 Then
            MsgBox("INgrese una Fecha", MsgBoxStyle.Critical, vbYes)
            cboResources.Focus()
            Return False
        End If

        If dgvActivities.Rows.Count = 0 Then
            MsgBox("Debe ingresar actividades", MsgBoxStyle.Critical, vbYes)
            Return False
        End If

        If dgvProjectMembers.Rows.Count = 0 Then
            MsgBox("Debe ingresar miembros", MsgBoxStyle.Critical, vbYes)
            Return False
        End If

        Return True
    End Function

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim row As DataGridViewRow
        Dim projectMemberList As New ArrayList
        Dim projectActivityList As New ArrayList

        If validateRequiredFields() = True Then
            project.nameProperty = Me.txtProjectName.Text
            project.descriptionProperty = Me.rtxtDescription.Text
            project.estimatedFromDateProperty = CDate(Me.dtpStartDate.Value.ToString("dd/MM/yyyy"))
            project.estimatedToDateProperty = CDate(Me.dtpEndDate.Value.ToString("dd/MM/yyyy"))
            Dim projectResponsibiliId As Integer = cboProjectResponsibility.SelectedValue
            project.projectResponsibleProperty = New Resource(projectResponsibiliId)

            If project.idProperty = 0 Then
                project.createDateProperty = Date.Now
                Dim state As New State
                state.idProperty = state.PROJECT_STATE_IN_PLANNING_ID
                project.stateProperty = state
            End If

            For Each row In dgvProjectMembers.Rows
                Dim availableFromDate As Date = CDate(row.Cells("dateFrom").Value)
                Dim availableToDate As Date = CDate(row.Cells("dateTo").Value)
                Dim resourceId As String = row.Cells("id_resource").Value
                Dim resource As Resource = New Resource(resourceId)
                Dim rolId As String = row.Cells("Id_Rol").Value
                Dim role As Role = New Role(rolId)
                Dim projectMember As ProjectMember = New ProjectMember(Nothing, availableFromDate, availableToDate, resource, project, Nothing, Nothing)
                Dim addFlag = True

                For Each obj As ProjectMember In projectMemberList
                    If (obj.resourceProperty.idProperty = resourceId) Then
                        projectMember = obj
                        ' Como ya existe no lo agregaré a la lista
                        addFlag = False
                    End If
                Next
                Dim projectMemberRole As ProjectMemberRole = New ProjectMemberRole(Nothing, projectMember, role)
                projectMember.projectMemberRoleProperty.Add(projectMemberRole)

                ' Si ya existe no debo agregarlo
                If (addFlag) Then
                    projectMemberList.Add(projectMember)
                End If
            Next
            Me.project.projectMembersProperty = projectMemberList

            For Each row In dgvActivities.Rows
                Dim estimated_start_date As Date = CDate(row.Cells("estimated_start_date").Value)
                Dim estimated_end_date As Date = CDate(row.Cells("estimated_end_date").Value)
                Dim start_date As Date
                Dim end_date As Date

                If IsDate(row.Cells("start_date").Value) Then
                    start_date = CDate(row.Cells("start_date").Value)
                Else
                    start_date = Nothing
                End If

                If IsDate(row.Cells("end_date").Value) Then
                    end_date = CDate(row.Cells("end_date").Value)
                Else
                    end_date = Nothing
                End If

                Dim project_id As Integer = CInt(row.Cells("project_id").Value)
                Dim activity_id As Integer = CInt(row.Cells("activity_id").Value)
                Dim activityBean As Activity = New Activity(activity_id)

                Dim projectActivity As ProjectActivity = New ProjectActivity(Nothing, estimated_start_date, estimated_end_date, start_date, end_date, activityBean, project)
                projectActivityList.Add(projectActivity)
            Next
            Me.project.projectActivitiesProperty = projectActivityList

            ProjectAdministrator.save(project)

            Me.Close()
            SearchProjectsForm.Show()
        End If
    End Sub


    Private Sub cmdAgregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAgregar.Click
        If Me.dgvActivities.Rows.Count > 0 Then
            For indice As Integer = 0 To Me.dgvActivities.Rows.Count - 1
                If Me.cboActivity.SelectedValue = Me.dgvActivities.Rows(indice).Cells("activity_id").Value Then
                    MsgBox("Ya se cargo la actividad " & Me.cboActivity.Text)
                    Exit Sub
                End If
            Next
        End If
        Me.dgvActivities.Rows.Add(Nothing, Nothing, Me.cboActivity.SelectedValue, Me.cboActivity.Text, _
                         Me.dtpEstimatedStartDate.Value.ToString("dd/MM/yyyy"), _
                         Me.dtpEstimatedEndDate.Value.ToString("dd/MM/yyyy"), _
                         Me.dtpRealStartDate.Text, _
                         Me.dtpRealEndDate.Text)
    End Sub

    Private Sub cmdQuitar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdQuitar.Click
        If Me.dgvActivities.Rows.Count > 0 Then
            Me.dgvActivities.Rows.Remove(Me.dgvActivities.CurrentRow)
        End If
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Dim Result As DialogResult

        Result = MessageBox.Show(Me, "Esta seguro que desea salir?", "Atencion", MessageBoxButtons.YesNo, _
            MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If Result = System.Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click
        If Me.dgvProjectMembers.Rows.Count > 0 Then
            Me.dgvProjectMembers.Rows.Remove(Me.dgvProjectMembers.CurrentRow)
        End If
    End Sub

    Private Sub dtpStartDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpStartDate.ValueChanged
        configurarRangoFechas(dtpStartDate, dtpEndDate)
    End Sub

    Private Sub dtpEndDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpEndDate.ValueChanged
        configurarRangoFechas(dtpStartDate, dtpEndDate)
    End Sub

    Private Sub dtpFromDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFromDate.ValueChanged
        configurarRangoFechas(dtpFromDate, dtpToDate)
    End Sub

    Private Sub dtpToDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpToDate.ValueChanged
        configurarRangoFechas(dtpFromDate, dtpToDate)
    End Sub

    Private Sub dtpEstimatedStartDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpEstimatedStartDate.ValueChanged
        configurarRangoFechas(dtpEstimatedStartDate, dtpEstimatedEndDate)
    End Sub

    Private Sub dtpEstimatedEndDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpEstimatedEndDate.ValueChanged
        configurarRangoFechas(dtpEstimatedStartDate, dtpEstimatedEndDate)
    End Sub

    Private Sub dtpRealStartDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'configurarRangoFechas(dtpRealStartDate, dtpRealEndDate)
    End Sub

    Private Sub dtpRealEndDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'configurarRangoFechas(dtpRealStartDate, dtpRealEndDate)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim state As New State
        state.idProperty = state.PROJECT_STATE_IN_PROCESS_ID
        project.stateProperty = state
        ProjectAdministrator.save(project)
    End Sub

    Private Function getProyectState() As ProjectStateForm
        Return StateProjectFormCreator.getStateProjectForm(Me)
        'If project.stateProperty Is Nothing Then
        'Return New StateProjectFormCreating(Me)
        'ElseIf project.stateProperty.idProperty = State.PROJECT_STATE_IN_PLANNING_ID Then
        'Return New StateProjectFormUpdate(Me)
        'ElseIf project.stateProperty.idProperty = State.PROJECT_STATE_IN_PROCESS_ID Then
        'Return New StateProjectFormQueried(Me)
        'End If
    End Function

    Private Sub cmdStartProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartProject.Click
        Dim Result As DialogResult
        Result = MessageBox.Show(Me, "Esta seguro que desea comenzar el proyecto? Se guardaran los cambios", "Atencion", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

        If Result = System.Windows.Forms.DialogResult.Yes Then
            Me.project.stateProperty.idProperty = 2
            ProjectAdministrator.startProject(Me.project)
            Me.cmdSave_Click(Nothing, Nothing)
        End If
    End Sub

    Private Sub cmdCancelProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelProject.Click
        Dim Result As DialogResult
        Result = MessageBox.Show(Me, "Esta seguro que desea cancelar el proyecto? Se guardaran los cambios", "Atencion", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

        If Result = System.Windows.Forms.DialogResult.Yes Then
            Me.project.stateProperty.idProperty = 4
            ProjectAdministrator.startProject(Me.project)
            Me.cmdSave_Click(Nothing, Nothing)
        End If
    End Sub
End Class